//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_mapclickrespawn.sqf"
#include "xr_macros.sqf"

private ["_respawn_pos","_etime", "_mhq", "_mhqobj", "_getcusbp"];
_respawn_pos = _this;
__TRACE_1("","_respawn_pos");
__pSetVar [QGVARXR(hasusedmapclickspawn), true];
__TRACE(", black out");
172 cutText [(localize "STR_DOM_MISSIONSTRING_917"),"BLACK OUT", 0.2];
sleep 1;
__TRACE("stopspect = true");
GVARXR(stopspect) = true;
player setVariable [QGVARXR(pluncon), false, true];
[QGVARXR(setCap), [player, false]] call d_fnc_NetCallEvent;
//[QGVARXR(wn), [player,105]] call d_fnc_NetCallEvent;
sleep 0.5;

_getcusbp = {
	if (!isNil "d_custom_layout" && {count d_custom_layout > 0}) then {
		call d_fnc_retrieve_layoutgear;
		_cusbp = player getVariable ["d_custom_backpack", []];
		if (count _cusbp > 0) then {
			__pSetVar ["d_player_backpack", _cusbp];
		};
	};
};

_nos = _respawn_pos nearEntities ["All", 25];
__TRACE_1("","_nos");
_mhqobj = objNull;
{
	if (_x getVariable ["d_vec_type", ""] == "MHQ") exitWith {
		_mhqobj = _x;
	};
} forEach _nos;
[QGVARXR(wn), [player, 105]] call d_fnc_NetCallEvent;
__TRACE_1("","_mhqobj");
if (!isNull _mhqobj) then {
	_newppos = _mhqobj modelToWorld [0,-7,0];
	player setDir (direction _mhqobj);
	player setPosATL [_newppos select 0, _newppos select 1, 0];
	_nobs = nearestObjects [player,([[d_the_box],d_mhq_vehicle_types] call d_fnc_arrayPushStack2), 30];
	{player reveal _x} forEach _nobs;
	call _getcusbp;
} else {
	call _getcusbp;
	if (surfaceIsWater _respawn_pos) then {
		player setPosASL [markerpos "base_spawn_1" select 0, markerpos "base_spawn_1" select 1, 16.20];
	} else {
		player setpos _respawn_pos;
	};
};
player setDamage 0;
// TYRGHEN
if (GVARXR(respawn_score_penalty) > 0) then {
	_val = 0 - GVARXR(respawn_score_penalty);
	["d_pas", [player, _val]] call d_fnc_NetCallEventCTS;
	systemChat format [(localize "STR_DOM_MISSIONSTRING_2021"), GVARXR(respawn_score_penalty)];
};
__TRACE("MapClickRespawn, black in");
172 cutText [(localize "STR_DOM_MISSIONSTRING_918"),"BLACK IN", 6];
if (GVARXR(max_lives) != -1) then {
	0 spawn {
		sleep 7;
		hintSilent format [(localize "STR_DOM_MISSIONSTRING_933"), __pGetVar(GVARXR(lives))];
	};
};
__TRACE("MapClickRespawn done");